package com.magic.uschat.adapter;

import com.lidroid.xutils.ViewUtils;
import com.magic.uschat.BaseActivity;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder implements OnClickListener,OnLongClickListener{

	protected OnRecyclerViewListener onRecyclerViewListener;
	protected Context context;
	
	public BaseViewHolder(Context context,ViewGroup root,int layoutRes,OnRecyclerViewListener onRecyclerViewListener) {
		super(LayoutInflater.from(context).inflate(layoutRes, root, false));
		this.context = context;
		this.onRecyclerViewListener = onRecyclerViewListener;
		ViewUtils.inject(this, itemView);
		itemView.setOnClickListener(this);
		itemView.setOnLongClickListener(this);
	}
	
	public Context getContext(){
		return itemView.getContext();
	}
	
	public abstract void bindData(T t);
	
	 private Toast toast;
	  public void toast(final Object obj) {
	    try {
	      ((BaseActivity)context).runOnUiThread(new Runnable() {

	        @Override
	        public void run() {
	          if (toast == null)
	            toast = Toast.makeText(context,"", Toast.LENGTH_SHORT);
	          toast.setText(obj.toString());
	          toast.show();
	        }
	      });
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	  }
	
	@Override
	public boolean onLongClick(View v) {
		if (null!=onRecyclerViewListener) {
			return onRecyclerViewListener.onItemLongClick(getAdapterPosition());
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		if (null!=onRecyclerViewListener) {
			onRecyclerViewListener.onItemClick(getAdapterPosition());
		}
	}

}
